package com.gaojiawei.multiThread16_JUCUtil;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * countDownLatch use demo
 */
public class L01_CountDownLatch {
    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    private static CountDownLatch countDownLatch = new CountDownLatch(3);
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main service start");
        fixedThreadPool.execute(()->aa());
        fixedThreadPool.execute(()->bb());
        fixedThreadPool.execute(()->{
            cc();
        });
        countDownLatch.await();
        System.out.println("finish main");

    }

    private static void aa(){
        System.out.println("execute a service");
        try {
            Thread.sleep(1000);
            System.out.println("finish a service");
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void bb(){
        System.out.println("execute b service");
        try {
            Thread.sleep(2000);
            System.out.println("finish b service");
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void cc(){
        System.out.println("execute c service");
        try {
            Thread.sleep(3000);
            System.out.println("finish c service");
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }





}
